const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    hidden: true
  },
  onLoad() {
    var that = this
    let list = [];
    let colleague = [[{}]];
    let colleagueList = [[{}]]
    wx.request({
      url: 'http://localhost:8081/test/colleague',
      data:{
        id:app.globalData.employee.id
      },
      method:"POST",
      success(res){
        for (let i = 0; i < 26; i++) {          
          switch (i){
            case 0: colleague[i] = res.data.data.key.A
            break;
            case 1: colleague[i] = res.data.data.key.B
            break;
            case 2: colleague[i] = res.data.data.key.C
            break;
            case 3: colleague[i] = res.data.data.key.D
            break;
            case 4: colleague[i] = res.data.data.key.E
            break;
            case 5: colleague[i] = res.data.data.key.F
            break;
            case 6: colleague[i] = res.data.data.key.G
            break;
            case 7: colleague[i] = res.data.data.key.H
            break;
            case 8: colleague[i] = res.data.data.key.I
            break;
            case 9: colleague[i] = res.data.data.key.J
            break;
            case 10: colleague[i] = res.data.data.key.K
            break;
            case 11: colleague[i] = res.data.data.key.L
            break;
            case 12: colleague[i] = res.data.data.key.M
            break;
            case 13: colleague[i] = res.data.data.key.N
            break;
            case 14: colleague[i] = res.data.data.key.O
            break;
            case 15: colleague[i] = res.data.data.key.P
            break;
            case 16: colleague[i] = res.data.data.key.Q
            break;
            case 17: colleague[i] = res.data.data.key.R
            break;
            case 18: colleague[i] = res.data.data.key.S
            break;
            case 19: colleague[i] = res.data.data.key.T
            break;
            case 20: colleague[i] = res.data.data.key.U
            break;
            case 21: colleague[i] = res.data.data.key.V
            break;
            case 22: colleague[i] = res.data.data.key.W
            break;
            case 23: colleague[i] = res.data.data.key.X
            break;
            case 24: colleague[i] = res.data.data.key.Y
            break;
            case 25: colleague[i] = res.data.data.key.Z
            break
          }
        }
        let k=0
        for(let i=0;i<26;i++){
          if(colleague[i]!=null){
            colleagueList[k] = colleague[i]
            list[k] = String.fromCharCode(65 + i)
            k++
          }
        }
        that.setData({
          list: list,
          listCur: list[0],
          colleague: colleagueList
        })
        console.log(colleague[3][0].name)
      }
    })
    
   
  },
  onReady() {
    let that = this;
    wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function(res) {
      that.setData({
        boxTop: res.top
      })
    }).exec();
    wx.createSelectorQuery().select('.indexes').boundingClientRect(function(res) {
      that.setData({
        barTop: res.top
      })
    }).exec()
  },
  //获取文字信息
  getCur(e) {
    this.setData({
      hidden: false,
      listCur: this.data.list[e.target.id],
    })
  },

  setCur(e) {
    this.setData({
      hidden: true,
      listCur: this.data.listCur
    })
  },
  //滑动选择Item
  tMove(e) {
    let y = e.touches[0].clientY,
      offsettop = this.data.boxTop,
      that = this;
    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      let num = parseInt((y - offsettop) / 20);
      this.setData({
        listCur: that.data.list[num]
      })
    };
  },

  //触发全部开始选择
  tStart() {
    this.setData({
      hidden: false
    })
  },

  //触发结束选择
  tEnd() {
    this.setData({
      hidden: true,
      listCurID: this.data.listCur
    })
  },
  indexSelect(e) {
    let that = this;
    let barHeight = this.data.barHeight;
    let list = this.data.list;
    let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
    for (let i = 0; i < list.length; i++) {
      if (scrollY < i + 1) {
        that.setData({
          listCur: list[i],
          movableY: i * 20
        })
        return false
      }
    }
  }
});